package com.joysuch.wwyt.bp.service.impl;

import com.alibaba.fastjson.JSON;
import com.joysuch.wwyt.bp.bean.BpEnterpriseCertBean;
import com.joysuch.wwyt.bp.entity.BpEnterpriseCert;
import com.joysuch.wwyt.bp.repository.BpEnterpriseCertDao;
import com.joysuch.wwyt.bp.service.BpEnterpriseCertService;
import com.joysuch.wwyt.core.bean.BaseAttachmentBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.repository.BaseTypeGroupDao;
import com.joysuch.wwyt.core.repository.BaseTypeGroupItemDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.util.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ZhangDong
 * @Date 2021/1/14 9:32
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class BpEnterpriseCertServiceImpl implements BpEnterpriseCertService {

    @Autowired
    private BpEnterpriseCertDao dao;
    @Autowired
    private BaseTypeGroupDao baseTypeGroupDao;
    @Autowired
    private BaseTypeGroupItemDao baseTypeGroupItemDao;

    @Override
    public ResultBean save(BpEnterpriseCertBean bean) {
        Long id = bean.getId();
        BpEnterpriseCert cert;
        if (id == null) {
            cert = new BpEnterpriseCert();
        } else {
            cert = dao.findById(id).orElseThrow(() -> new IllegalArgumentException("此数据不存在"));
        }
        BeanUtils.copyProperties(bean, cert);
        //证件附件
        BaseAttachmentBean certAttachment = bean.getCertAttachment();
        cert.setCertAttachmentJson(certAttachment == null ? null : JSON.toJSONString(certAttachment));
        //照片
        List<BaseAttachmentBean> imgAttachmentList = bean.getImgAttachmentList();
        cert.setImgAttachmentJson(imgAttachmentList == null ? null : JSON.toJSONString(imgAttachmentList));
        BpEnterpriseCert save = dao.save(cert);
        return ResultBean.success(save.getId());
    }

    @Override
    public ResultBean delete(Long id) {
        Assert.notNull(id, "id cannot be null");
        dao.deleteById(id);
        return ResultBean.defaultSuccessResult();
    }

    @Override
    public ResultBean get(Long id) {
        Assert.notNull(id, "id cannot be null");
        BpEnterpriseCert cert = dao.findById(id).orElseThrow(() -> new IllegalArgumentException("此数据不存在"));
        return ResultBean.success(convert(cert));
    }

    @Override
    public ResultBean page(Pageable pageable) {
        pageable = PageDataUtils.addOrderByDesc(pageable, "id");
        Page<BpEnterpriseCert> page = dao.findAll(pageable);
        List<BpEnterpriseCert> list = page.getContent();
        List<BpEnterpriseCertBean> resultList = new ArrayList<>(list.size());
        for (BpEnterpriseCert cert : list) {
            resultList.add(convert(cert));
        }
        return ResultBean.pageData(resultList, page.getTotalElements());
    }

    private BpEnterpriseCertBean convert(BpEnterpriseCert cert) {
        BpEnterpriseCertBean bean = new BpEnterpriseCertBean();
        BeanUtils.copyProperties(cert, bean);
        //证件附件
        String certAttachmentJson = cert.getCertAttachmentJson();
        bean.setCertAttachment(certAttachmentJson == null ? null : JSON.parseObject(certAttachmentJson, BaseAttachmentBean.class));
        //照片
        String imgAttachmentJson = cert.getImgAttachmentJson();
        bean.setImgAttachmentList(imgAttachmentJson == null ? null : JSON.parseArray(imgAttachmentJson, BaseAttachmentBean.class));
        //证书类型
        Long certTypeId = cert.getCertTypeId();
        bean.setCertTypeName(certTypeId == null ? null : baseTypeGroupItemDao.findNameById(certTypeId));
        return bean;
    }

}
